package com.hrms.config.security.handler;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hrms.config.security.exception.CustomerAuthenticationException;
import com.hrms.utils.ResultVo;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Title: LoginFailureHandler
 * @Author zhanji
 * @Package com.hrms.config.security.handler
 * @description:
 */
@Component("loginFailureHandler")
public class LoginFailureHandler implements AuthenticationEntryPoint {
    private final HttpServletResponse httpServletResponse;

    public LoginFailureHandler(HttpServletResponse httpServletResponse) {
        this.httpServletResponse = httpServletResponse;
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        int code=500;
        String str ="";
        if(e instanceof AccountExpiredException){
            str ="账户过期请重新登录";
        } else if (e instanceof BadCredentialsException) {
            str ="用户名或密码错误，登录失败";
        }else if (e instanceof CredentialsExpiredException) {
            str ="密码过期，登录失败";
        }else if (e instanceof DisabledException) {
            str ="账户被禁用，登录失败";
        }else if (e instanceof LockedException) {
            str ="账户被锁，登录失败";
        }else if (e instanceof InternalAuthenticationServiceException) {
            str ="账户不存在，登录失败";
        }else if (e instanceof CustomerAuthenticationException) {
            code =600;
            str  =e.getMessage();
        }else if (e instanceof InsufficientAuthenticationException) {
            str ="无权限访问资源";
        }else{
            str ="登录失败";
        }
        String res = JSONObject.toJSONString(new ResultVo(str,code,null), SerializerFeature.DisableCircularReferenceDetect);
        //设置返回格式
        httpServletResponse.setContentType("application/json;charset=utf-8");
        ServletOutputStream out = httpServletResponse.getOutputStream();
        out.write(res.getBytes(StandardCharsets.UTF_8));
        out.flush();
        out.close();
    }
}
